﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Microsoft.Web.UI.WebControls;
using Newtonsoft.Json;
using Ccit.Utils;

namespace Ccit.OA.NewsCenter
{
    public partial class news_column_tree : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定树时的参数类型
                hdType.Value = (null != Request.QueryString["Type"]) ? Request.QueryString["Type"].ToString() : "0";
                _hidTabId.Value = (null != Request.QueryString["TABID"]) ? Request.QueryString["TABID"].ToString() : "";
                _hidWhereClause.Value = Ccit.OA.Utils.SysInterface.GetPageDataRangeSQL("NEWS_A2", _hidTabId.Value);
            }
            initializePage();
        }

        #region private Methods
        private void initializePage()
        {
            createTree();
            tvColumn.ExpandLevel = 1;
        }

        private void createTree()
        {
            //创建树
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            createRootNode(ref oRoot);
            tvColumn.Nodes.Add(oRoot);
            createSubNodes(ref oRoot);
        }
        //创建跟节点
        private void createRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            oRoot.Text = "信息栏目";
            oRoot.ImageUrl = "../images/toolbar/Folder_open.gif";
            oRoot.NavigateUrl = "";
            oRoot.Target = "iFrmList";
            oRoot.ID = "0_0";
        }
        //创建子节点
        private void createSubNodes(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            ColumnBLL bll = new ColumnBLL();

            string hasChild = "";
            Microsoft.Web.UI.WebControls.TreeNode oTreeNode;
            List<MsColumn> mclist = bll.GetTreeData(hdType.Value);
            foreach (MsColumn mc in mclist)
            {
                if (mc.columnLevelCode.Length == 4)
                {
                    hasChild = (bll.isHasChildType(mc.columnLevelCode)) ? "1" : "0";
                    oTreeNode = createTreeNode(mc.columnName, mc.columnId, hasChild);
                    oRoot.Nodes.Add(oTreeNode);
                }
            }
        }

        private Microsoft.Web.UI.WebControls.TreeNode createTreeNode(string columnName, string columnId, string hasChild)
        {
            Microsoft.Web.UI.WebControls.TreeNode oNode = new Microsoft.Web.UI.WebControls.TreeNode();

            ColumnBLL bll = new ColumnBLL();
            string sRangeColIds = bll.GetRangeColumn(_hidWhereClause.Value);
            _hidRangColIds.Value = sRangeColIds;
            if (sRangeColIds != "" && sRangeColIds.Contains(columnId))
            {
                oNode.Text = columnName;

                string sPageParam = "columnId=" + columnId + "&oper=1&Type=" + hdType.Value + "&TABID=" + _hidTabId.Value;//操作：0-新增，1-编辑
                string encodeParam = base64encode(sPageParam);
                oNode.NavigateUrl = "./news_column_edit.aspx?" + encodeParam;
            }
            else
            {
                oNode.Text = columnName;
                oNode.DefaultStyle["color"] = "red";
                oNode.NavigateUrl = "";
            }

            oNode.ImageUrl = "../images/toolbar/filelogo.gif";
            oNode.Target = "iFrmEdit";
            oNode.ID = columnId + "_" + "0";
            oNode.Type = "root";

            if (hasChild == "1")
            {
                Microsoft.Web.UI.WebControls.TreeNode oSubNode = new Microsoft.Web.UI.WebControls.TreeNode();
                oNode.Nodes.Add(oSubNode);
            }
            return oNode;
        }

        private string base64encode(string sPageParam)
        {
            string encodeParam = string.Empty;
            if (string.IsNullOrEmpty(sPageParam)) return encodeParam;

            encodeParam = Base64Lib.Encode(sPageParam);
            return encodeParam;
        }

        #endregion
        #region Ajax Methods
        /// <summary>
        /// get all sub asset types
        /// </summary>
        /// <param name="astTypeId"></param>
        /// <param name="nodeIndex"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string GetSubTypesByColumnId(string columnId, string nodeIndex, string expandSign, string levelStruct)
        {
            ColumnBLL bll = new ColumnBLL();
            List<MsColumn> list = bll.GetSubColumnTypes(columnId);
            string hasChild = "";
            List<MsColumn> rlist = new List<MsColumn>();
            foreach (MsColumn mc in list)
            {
                hasChild = (bll.isHasChildType(mc.columnLevelCode)) ? "1" : "0";
                mc.hasChild = hasChild;
                rlist.Add(mc);
            }
            string strResult = "GetSubTypesByColumnId" + "_" + JavaScriptConvert.SerializeObject(rlist) + "_" + nodeIndex + "_" + expandSign + "_" + levelStruct + "_" + columnId;
            return strResult;
        }
        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="columnId"></param>
        /// <param name="nodeIndex"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DeleteColumn(string columnId, string nodeIndex)
        {
            ColumnBLL bll = new ColumnBLL();
            List<string> slist = bll.RemoveNode(columnId);
            var oResult = new { ores = slist[0], oMes = slist[1], onodeIndex = nodeIndex };
            string strResult = "DeleteColumn" + "_" + JavaScriptConvert.SerializeObject(oResult);
            return strResult;
        }

        [global::System.Web.Services.WebMethod]
        public static string GetNodeLevelStruct(string dtNodeId)
        {
            ColumnBLL bll = new ColumnBLL();
            string levelStruct = bll.GetNodeLevelStruct(dtNodeId);
            var oResult = new { resultIds = levelStruct };
            string strResult = "GetNodeLevelStruct" + "_" + JavaScriptConvert.SerializeObject(oResult);
            return strResult;
        }
        #endregion
    }
}
